/*
 * @Descripttion: 
 * @version: 
 * @Author: mmzhang
 * @Date: 2021-07-23 09:45:29
 * @LastEditTime: 2023-02-17 15:19:41
 */

import table from '@/components/dt/table/render.js'
import nameOpt from '@/biz/option/name.js'
import dateOpt from '@/biz/option/date.js'
import houseOpt from '@/biz/option/house.js'
import Opt from '@/components/dt/info/Opt.js'
import dt from '@dt/dt'

let _this = null
let reg = /^[\d,-]?\d+(\.\d{1,2}){0,1}$/

class Column extends Opt {
  constructor(vm) {
    super()
    _this = vm
    this.build()
  }

  build() {
    this.createAll()
    this.opts.push(...this.all)
  }

  createAll() {
    this.all = [
    {
      title: '',
      minWidth: 50,
      type: 'checkbox',
      fixed: 'left'
    },
    {
      title: '认购单编号',
      field: 'orderCode',
      minWidth: 170,
      header: {
        filter: {
          title: '认购单编号',
          key: 'orderCode',
          placeholder: '请输入至少4位认购单编号',
          input: {},
          check: (val) => {
            if (val.orderCode.length < 4) {
              _this.$Message.warning('请输入至少4位认购单编号。')
            }
            return !(val.orderCode.length < 4)
          }
        },
      },
      slots: {
        default: (params, h) => {
          if (params.row.isBreakLowPrice == 1) {
            return table.cell.text(h, {
              value: params.row.orderCode + '(底)'
            })
          } else {
            return table.cell.text(h, {
              value: params.row.orderCode
            })
          }
        }
      }
    },
    {
      title: '认购房源',
      field: 'roomName',
      minWidth: 180,
      header: {
        filter: houseOpt.createRoom({
          key: 'roomId',
          title: '认购房源'
        })
      }
    },
    {
      field: 'customerName',
      minWidth: 150,
      title: '客户姓名',
      slots: {
        default: (params, h) => {
          return table.cell.btn(_this, h, {
            title: params.row.customerName,
            event: 'onLinkUser',
            data: params.row
          })
        }
      },
      header: {
        filter: {
          title: '客户姓名',
          key: 'customerName',
          placeholder: '请输入客户姓名',
          input: {},
        },
      }
    },
    {
      title: '客户手机号',
      field: 'customerPhone',
      minWidth: 150,
      header: {
        filter: {
          title: '客户手机号',
          key: 'customerPhone',
          placeholder: '请输入至少4位客户手机号',
          input: {},
          check: (val) => {
            if (val.customerPhone.length < 4) {
              _this.$Message.warning('请输入至少4位客户手机号。')
            }
            return !(val.customerPhone.length < 4)
          }
        },
      }
    },
    {
      field: 'counselorName',
      minWidth: 130,
      header: {
        filter: nameOpt.createPushPeople({
          key: 'counselorId',
          title: '归属顾问',
          to: (info) => {
            return {
              counselorId: info.userId
            }
          }
        })
      }
    },
    {
      field: 'productName',
      title: '所属产品',
      minWidth: 150,
      cell: {
        text: {
          lineClamp: 2,
          webkitBoxOrient: 'vertical',
          display: '-webkit-box',
          whiteSpace: 'inherit'
        }
      }
    },
    {
      title: '当前面积（㎡）',
      field: 'currentAdjustArea',
      minWidth: 150,
    },
    {
      title: '当前认购总价(元)',
      field: 'currentAdjustPrice',
      minWidth: 150,
    },
    {
      title: '调整后面积(㎡)',
      field: 'afterAdjustArea',
      minWidth: 150,
      slots: {
        default: (params, h) => {
          if (params.row.editFlag) {
            return table.cell.input(_this, h, {
              key: 'afterAdjustArea',
              type: 'number',
              placeholder: '请输入调整后面积',
              data: params.row,
              blur: (key) => {
                if (params.row.afterAdjustArea < 0) {
                  _this.$Message.error('数值不能小于0')
                  return false
                }

                if (params.row.afterAdjustArea && !reg.test(params.row
                    .afterAdjustArea)) {
                  _this.$Message.error('数值只能保留两位小数')
                  return false
                }

                // 计算调整面积差
                _this.calculationFun(params.row)

              }
            })
          } else {
            return table.cell.text(h, {
              value: params.row.afterAdjustArea
            })
          }
        }
      }
    },
    {
      title: '调整后认购总价(元)',
      field: 'afterAdjustPrice',
      minWidth: 150,
      slots: {
        default: (params, h) => {
          if (params.row.editFlag) {
            return table.cell.input(_this, h, {
              key: 'afterAdjustPrice',
              type: 'number',
              data: params.row,
              placeholder: '请输入调整后认购总价',
              blur: (key) => {
                if (params.row.afterAdjustPrice < 0) {
                  _this.$Message.error('数值不能小于0')
                  return false
                }

                if (params.row.afterAdjustPrice && !reg.test(params.row
                    .afterAdjustPrice)) {
                  _this.$Message.error('数值只能保留两位小数')
                  return false
                }
                // 计算调整总价差
                _this.calculationFun(params.row)
              }
            })
          } else {
            return table.cell.text(h, {
              value: params.row.afterAdjustPrice
            })
          }
        }
      }
    },
    {
      title: '调整面积差(㎡)',
      field: 'areaDV',
      minWidth: 150,
      slots: {
        default: (params, h) => {
          let color = params.row.areaDV > 0 ? '#D9001B' : params.row.areaDV < 0 ?
            '#70B603' : '#515a6e'

          return table.cell.text(h, {
            value: params.row.areaDV > 0 ? '+' + params.row.areaDV : params.row
              .areaDV,
            color: color
          })
        }
      }
    },
    {
      title: '调整总价差(元)',
      field: 'priceDV',
      minWidth: 150,
      slots: {
        default: (params, h) => {
          let color = params.row.priceDV > 0 ? '#D9001B' : params.row.priceDV < 0 ?
            '#70B603' : '#515a6e'

          return table.cell.text(h, {
            value: params.row.priceDV > 0 ? '+' + params.row.priceDV : params.row
              .priceDV,
            color: color
          })
        }
      }
    },
    {
      field: 'subscriptionDate',
      minWidth: 150,
      title: '认购日期',
      header: {
        filter: dateOpt.range({
          title: '认购日期',
          startKey: 'subscriptionStartDate',
          endKey: 'subscriptionEndDate',
          disabled: (date) => {
            return date.valueOf() > new Date().getTime()
          },
          check: (val) => {
            if (_this.$tool.dateDiff(val.subscriptionStartDate, val
                .subscriptionEndDate).Y >= 2) {
              // 日期最大跨度两年
              _this.$Message.warning('认购日期跨度至多两年。')
            }
            return !(_this.$tool.dateDiff(val.subscriptionStartDate, val
              .subscriptionEndDate).Y >= 2)
          }
        }),
      },
    },
    {
      field: 'contractorName',
      title: '认购人',
      minWidth: 130,
    },
    {
      title: '认购状态',
      field: 'status',
      minWidth: 130,
      cell: (item) => {
        return {
          text: {
            type: 'dict',
            key: 'subscriptionStatus',
            color: item.status == 2 ? '#D9001B' : '#515a6e'
          }
        }
      },
      header: {
        filter: {
          title: '认购状态',
          key: 'statusList',
          placeholder: '请选择认购状态',
          select: {
            multiple: true,
            options: dt.dictInfo().subscriptionStatus.filter(item => {
              return item.key ==
                4 || item.key == 7
            })
          },
        },
      },
    },
    {
      field: 'handles',
      title: '操作',
      width: 100,
      fixed: 'right',
      slots: {
        default: (params, h) => {
          let btns = []
          btns.push({
            title: params.row.editFlag ? '保存' : '',
            event: 'saveFun',
            data: params.row
          })

          btns.push({
            title: !params.row.editFlag ? '修改订单' : '',
            event: 'editFun',
            data: params.row
          })

          btns.push({
            title: params.row.editFlag ? '取消' : '',
            event: 'cancelFun',
            data: params.row
          })

          return table.cell.btns(_this, h, btns)
        }
      }
    }]
  }

}

export default Column